﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using smp.ButterflyCakes.Models.Data;

namespace smp.ButterflyCakes.Models.FormModels.Order
{
    public class ComponentButterCreamModel
    {
        [Required]
        public Guid OrderId { get; set; }
        [Required]
        public Guid ComponentId { get; set; }

        public bool IsButterCreamValid { get; private set; }
        public Guid? ButterCreamId { get; set; }
        public SelectItem SelectButterCream { get; private set; }
        
        public string ButterCreamColour { get; set; }

        public bool IsGanacheValid { get; private set; }
        public Guid? GanacheId { get; set; }
        public SelectItem SelectGanache { get; private set; }

        public void AddFillings(List<FillingItem> items, List<IncompatibleItem> incompatibleItems)
        {
            SelectButterCream = new SelectItem("ButterCreamId", true);
            SelectGanache = new SelectItem("GanacheId", true);

            foreach(var item in items)
            {
                if(item.FillingTypeName ==Constants.ButterCream)
                {
                    if(incompatibleItems.Any(x => x.Id == item.Id))
                    {
                        SelectButterCream.AddOption(new SelectOption(item.Name, item.Id, ButterCreamId.HasValue && ButterCreamId == item.Id, true, "Not compatible with cake favour"));
                    }
                    else
                    {
                        SelectButterCream.AddOption(new SelectOption(item.Name, item.Id, ButterCreamId.HasValue && ButterCreamId == item.Id));
                    }
                }
                else if(item.FillingTypeName == Constants.Ganache)
                {
                    if (incompatibleItems.Any(x => x.Id == item.Id))
                    {
                        SelectGanache.AddOption(new SelectOption(item.Name, item.Id, ButterCreamId.HasValue && ButterCreamId == item.Id, true, "Not compatible with cake favour"));
                    }
                    else
                    {
                        SelectGanache.AddOption(new SelectOption(item.Name, item.Id, ButterCreamId.HasValue && ButterCreamId == item.Id));
                    }
                }
            }

            IsButterCreamValid = SelectButterCream.Options.Any(x => !x.IsDisabled);
            IsGanacheValid = SelectGanache.Options.Any(x => !x.IsDisabled);
        }
    }
}
